﻿using System;
using System.Data;
using eTMS.Controllers.GiayToSinhVienService;
using System.Collections.Generic;
namespace eTMS.Controllers
{
    public class GiayToSinhVienController : GiayToSinhVienControllerBase
    {
        public GiayToSinhVienController()
            : base()
        {
        }
        public void SaveGiayToSinhVien(List<int> LsLoaiGiayTo_ID, int SoLuong, List<DataRow> lsRowSinhVien)
        {
            List<GiayToSinhVienEntity> lsAddNew = new List<GiayToSinhVienEntity>();
            List<GiayToSinhVienEntity> lsUpdate = new List<GiayToSinhVienEntity>();
            foreach (int LoaiGiayTo_ID in LsLoaiGiayTo_ID)
            {
                foreach (DataRow r in lsRowSinhVien)
                {
                    List<GiayToSinhVienEntity> ls = new List<GiayToSinhVienEntity>(dao.SelectAllFilter("LoaiGiayTo_ID="+LoaiGiayTo_ID.ToString()+" AND SinhVien_ID="+r["SinhVien_ID"].ToString()));
                    if (ls.Count == 0)
                    {
                        ResetObject();
                        lsAddNew.Add(GiayToSinhVienObject);
                        GiayToSinhVienObject.SoLuong = SoLuong;
                        GiayToSinhVienObject.SinhVien_ID = ConvertInt(r["SinhVien_ID"]);
                        GiayToSinhVienObject.LoaiGiayTo_ID = LoaiGiayTo_ID;
                        GiayToSinhVienObject.GhiChu = "";
                        GiayToSinhVienObject.DaTra = false;
                        GiayToSinhVienObject.NgayTra = DateTime.Now;
                    }
                    else
                    {
                        ls[0].SoLuong = SoLuong;
                        lsUpdate.Add(ls[0]);
                    }
                }
            }
            dao.SaveList(lsAddNew.ToArray());
            dao.UpdateList(lsUpdate.ToArray());
        }
        public void RemoveGiayToSinhVien(List<int> LsLoaiGiayTo_ID, int SoLuong, List<DataRow> lsRowSinhVien)
        {
            List<int> ls = new List<int>();
            foreach (DataRow r in lsRowSinhVien)
            {
                ls.Add(ConvertInt(r["SinhVien_ID"]));
            }
            foreach (int LoaiGiayTo_ID in LsLoaiGiayTo_ID)
            {
                dao.DeleteByLoaiGiayToListSinhVien_ID(ls.ToArray(), LoaiGiayTo_ID);
            }
        }
        public void LoadAllDSGiayToSinhVien(int SinhVien_ID)
        {
            Table = dao.SelectAllDSWithLoaiGiayTo(SinhVien_ID).Tables[0];
            AddIndexToTable();
            foreach (DataRow r in Table.Rows)
            {
                if (!ConvertBool(r["DaTra"]))
                {
                    r["NgayTra"] = System.DBNull.Value;
                }
            }
        }
        public void TraGiayToHienTai()
        {
            foreach (DataRow r in this.Table.Rows)
            {
                r["DaTra"] = true;
                if (r.IsNull("NgayTra"))
                {
                    r["NgayTra"] = DateTime.Now;
                }
            }
            UpdateGiayToSinhVien();
        }
        public void UpdateGiayToSinhVien()
        {
            List<GiayToSinhVienEntity> lsUpdate = new List<GiayToSinhVienEntity>();
            foreach (DataRow r in Table.Rows)
            {
                lsUpdate.Add(MapDataRowToObject(r));
                if (lsUpdate[lsUpdate.Count - 1].GhiChu == null)
                    lsUpdate[lsUpdate.Count - 1].GhiChu = "";
            }
            dao.UpdateList(lsUpdate.ToArray());
        }
        public void TraGiayToByListSinhVien(List<int> LsLoaiGiayTo_ID, List<DataRow> lsRowSinhVien)
        {
            List<int> ls = new List<int>();
            foreach (DataRow r in lsRowSinhVien)
            {
                ls.Add(ConvertInt(r["SinhVien_ID"]));
            }
            foreach (int LoaiGiayTo_ID in LsLoaiGiayTo_ID)
            {
                dao.UpdateByLoaiGiayToListSinhVien_ID(ls.ToArray(), LoaiGiayTo_ID, true, DateTime.Now);
            }
        }
    }
}
